package com.xiaohao.communication.daoImpl;

import com.mongodb.BasicDBObject;
import com.mongodb.client.result.DeleteResult;
import com.xiaohao.communication.bean.Answer;
import com.xiaohao.communication.bean.PraiseDetails;
import com.xiaohao.communication.bean.UserTopic;
import com.xiaohao.communication.dao.UserTopicPageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserTopicDaoImpl implements UserTopicPageDao {

    @Autowired
    MongoTemplate mongoTemplate;

    //发布话题
    @Override
    public void publishTopic(UserTopic userTopic) {
        //获得当前最大的topicID
        Query query = new Query().with(new Sort(Sort.Direction.DESC, "topic_id"));
        List<UserTopic> userTopics = mongoTemplate.find(query, UserTopic.class, "userTopic");
        Integer currentMaxTopicIdInMongo = userTopics.get(0).getTopic_id();
        //加锁地插入
        synchronized (currentMaxTopicIdInMongo) {
            userTopic.setTopic_id(currentMaxTopicIdInMongo + 1);
            mongoTemplate.insert(userTopic, "userTopic");
        }


    }

    //删除话题
    @Override
    public void deleteTopic(int id) {
        DeleteResult remove = mongoTemplate.remove(new Query(Criteria.where("topic_id").is(id)), "userTopicPage");
        System.out.println(remove);
    }


    /**
     * 发布回答
     * 发布回答时，取出此话题的所有回答数，进行自加1,然后去更改数据库。
     */
    @Override
    public void publihAnswer(Answer answer) {
        //话题标志id
        int topic_id = answer.getTopic_id();

        //获取userTopic 集合中 answerNumber 属性值
        Query query = new Query(Criteria.where("topic_id").is(topic_id));
        List<UserTopic> userTopics = mongoTemplate.find(query, UserTopic.class);
        UserTopic userTopic = userTopics.get(0);
        int answerNumber = userTopic.getAnswerNumber();


        //更改 answerNumber 自加1
        Update update = new Update();
        update.set("answerNumber", answerNumber++);
        mongoTemplate.updateFirst(query, update, UserTopic.class, "userTopic");

        Answer save = mongoTemplate.save(answer);

    }

    //删除回答
    @Override
    public void deleteAnswer(int id) {
        DeleteResult answer_id = mongoTemplate.remove(new Query(Criteria.where("answer_id").is(id)), Answer.class);
    }


    /***************************************/

    //点赞            succes
    @Override
    public void addPraise(PraiseDetails praiseDetails) {
        mongoTemplate.save(praiseDetails);
    }

    //取消赞
    @Override
    public void deletePraise(int topic_id) {
        mongoTemplate.remove(new Query(Criteria.where("topic_id").is(topic_id)), "praiseDetails");
    }

    //模糊查询话题
    @Override
    public List<UserTopic> getUserTopic(String content) {
        Query query = new Query(Criteria.where("topicContent").regex(content));
        List<UserTopic> userTopics = mongoTemplate.find(query, UserTopic.class, "userTopic");


        return userTopics;

    }

    //获取的当前话题所有回答   根据id 返回指定字段的内容
    @Override
    public List<Answer> getCurrTopicAllAnswer(int topic_id) {


        //指定返回的字段
//        BasicDBObject fieldsObject = new BasicDBObject();
//        fieldsObject.put("answerContent", true);

        //查询条件
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put("topic_id", topic_id);

        Query query = new BasicQuery(dbObject.toJson());

        List<Answer> answer = mongoTemplate.find(query, Answer.class, "answer");

        return answer;


    }

    /*
     * 获取所有话题
     *
     * */

    @Override
    public List<UserTopic> getAllUserTopic() {

        List<UserTopic> userTopic = mongoTemplate.find(new Query().limit(20), UserTopic.class, "userTopic");

        return userTopic;
    }


    @Override
    public UserTopic getTopicByTopicId(int topic_id) {

        //查询条件
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put("topic_id", topic_id);
        Query query = new BasicQuery(dbObject.toJson());
        List<UserTopic> userTopic = mongoTemplate.find(query, UserTopic.class, "userTopic");
        return userTopic.get(0);

    }
}
